package com.fg.cloud.manager.service.factory.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.factory.ExpressCostsEntity;
import com.fg.cloud.manager.entry.factory.ExpressModelEntity;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.factory.ExpressCostsMapper;
import com.fg.cloud.manager.mapper.factory.ExpressModelMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.factory.ExpressModelService;

/**
 * Description:快递费模板Service
 * 
 * @author shaoqiping
 * @date 2017年9月18日上午10:47:15
 */
@Service
public class ExpressModelServiceImpl extends BaseServiceImpl implements ExpressModelService {

	@Autowired
	private ExpressModelMapper modelMapper;
	@Autowired
	private ExpressCostsMapper costsMapper;

	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return modelMapper;
	}

	@Override
	public ExpressModelEntity findByName(String name) {
		return modelMapper.selectByName(name);
	}

	@Override
	@Transactional
	public boolean removeModel(ExpressModelEntity entity) {
		int temp = modelMapper.deleteByPrimaryKey(entity);
		temp += costsMapper.delete(entity.getId());
		return temp > 0;
	}

	@Override
	@Transactional
	public boolean insertSelective(ExpressModelEntity entity, List<ExpressCostsEntity> costs) {
		int temp = modelMapper.insertSelective(entity);
		if (costs != null&&costs.size()>0) {
			for (ExpressCostsEntity expressCostsEntity : costs) {
				expressCostsEntity.setModelId(entity.getId());
				expressCostsEntity.setFacId(entity.getFacId());
			}
			temp += costsMapper.inserts(costs);
		}
		return temp > 0;
	}

	@Override
	@Transactional
	public boolean update(ExpressModelEntity entity, List<ExpressCostsEntity> costs) {
		int temp = modelMapper.updateByPrimaryKeySelective(entity);
		temp += costsMapper.delete(entity.getId());
		if (costs != null&&costs.size()>0) {
			for (ExpressCostsEntity expressCostsEntity : costs) {
				expressCostsEntity.setModelId(entity.getId());
				expressCostsEntity.setFacId(entity.getFacId());
			}
			temp += costsMapper.inserts(costs);
		}
		return temp > 0;
	}

	@Override
	public void findList(PageUtils<ExpressModelEntity> pageinfo) {
		long total = modelMapper.filterCount(pageinfo.getCondition());
		pageinfo.setTotal(total);
		if (total > 0) {
			pageinfo.setData(modelMapper.findList(pageinfo));
		} else {
			pageinfo.setData(new ArrayList<ExpressModelEntity>());
		}
	}
}
